package top.goldenyear.porpoise.system.permission.service;

import com.easy.query.api.proxy.client.EasyEntityQuery;
import com.easy.query.solon.annotation.Db;
import org.noear.solon.data.annotation.Ds;
import top.goldenyear.porpoise.common.core.util.tree.TreeUtils;
import top.goldenyear.porpoise.framework.common.enums.UserType;
import top.goldenyear.porpoise.framework.common.model.LoginUser;
import top.goldenyear.porpoise.framework.security.core.service.IPermissionService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.v7.core.collection.CollUtil;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;
import top.goldenyear.porpoise.framework.web.core.model.PageResult;
import top.goldenyear.porpoise.framework.web.core.model.query.QPage;
import top.goldenyear.porpoise.framework.web.core.service.BaseService;
import top.goldenyear.porpoise.system.account.model.Account;
import top.goldenyear.porpoise.system.account.service.AccountService;
import top.goldenyear.porpoise.system.permission.model.Permission;
import top.goldenyear.porpoise.system.permission.model.PermissionNode;
import top.goldenyear.porpoise.system.permission.model.proxy.PermissionProxy;

/**
 * 菜单权限表 Service 服务层
 *
 * @author airhead
 * @since 2024-10-17
 */
@Component
@Slf4j
public class PermissionService extends BaseService<Permission, PermissionProxy>
    implements IPermissionService {
  @Db
  private EasyEntityQuery easyEntityQuery;
  @Inject private AccountService accountService;

  public PermissionService() {
    super(Permission.class, PermissionProxy.class);
  }

  @Override
  public LoginUser getLoginUser(Integer loginId) {
    Account account = accountService.getById(loginId);
    if (account == null) {
      return null;
    }

    LoginUser loginUser = new LoginUser();
    loginUser.setId(account.getId());
    loginUser.setUserName(account.getUserName());
    loginUser.setNickName(account.getNickName());
    loginUser.setUserType(UserType.ADMIN.getValue());

    return loginUser;
  }

  @Override
  public List<String> getPermissionList(Object o, String s) {
    return List.of();
  }

  @Override
  public List<String> getRoleList(Object o, String s) {
    return List.of();
  }

  public Boolean createMenu(Permission permission) {
    return save(permission);
  }

  public Boolean updateMenu(Permission permission) {
    return updateById(permission);
  }

  public Permission getMenu(Integer id) {
    return getById(id);
  }

  public Boolean deleteMenu(Integer id) {
    return deleteById(id);
  }

  public List<Permission> listMenu(Permission permission) {
    return list(
        permissionProxy -> {
          List<Integer> types = new ArrayList<>();
          types.add(1);
          types.add(2);
          permissionProxy.type().in(types);
        });
  }

  public PageResult<Permission> paginateMenu(QPage qPage, Permission permission) {
    paginate(
        qPage,
        permissionProxy -> {
          List<Integer> types = new ArrayList<>();
          types.add(1);
          types.add(2);
          permissionProxy.type().in(types);
        });

    return paginate(qPage, permission);
  }

  public List<PermissionNode> treeMenu(List<Integer> types) {
    if (CollUtil.isEmpty(types)) {
      types = new ArrayList<>();
      types.add(1);
      types.add(2);
    }

    List<Integer> finalTypes = types;
    List<PermissionNode> list =
        listAs(
            permissionProxy -> {
              permissionProxy.type().in(finalTypes);
            },
            PermissionNode.class);

    List<PermissionNode> tree = TreeUtils.buildTree(list, 0);
    Collections.sort(tree);

    return tree;
  }

  public Boolean createButton(Permission permission) {
    return save(permission);
  }

  public Boolean updateButton(Permission permission) {
    return updateById(permission);
  }

  public Permission getButton(Integer id) {
    return getById(id);
  }

  public Boolean deleteButton(Integer id) {
    return deleteById(id);
  }

  public List<Permission> listButton(Permission permission) {
    return list(
        permissionProxy -> {
          permissionProxy.type().eq(3);
        });
  }

  public PageResult<Permission> paginateButton(QPage qPage, Permission permission) {
    if (permission.getParentId() == null || permission.getParentId() == 0) {
      return PageResult.empty();
    }

    return paginate(
        qPage,
        permissionProxy -> {
          permissionProxy.parentId().eq(permission.getParentId());
          permissionProxy.status().eq(permission.getStatus());
          permissionProxy.type().eq(3);
        });
  }

  public List<PermissionNode> getMenuTree() {
    List<PermissionNode> list = listAs(PermissionNode.class);
    return TreeUtils.buildTree(list, 0);
  }

  public List<String> getAllPages() {
    return new ArrayList<>();
  }

  @Override
  protected EasyEntityQuery easyEntityQuery() {
    return easyEntityQuery;
  }
}
